package models

import (
	orm "go-papers/database"
	"go-papers/tools"
	_ "time"
)

type MemberThirdInfo struct {
	Id          int    `json:"id" gorm:"type:int(11) unsigned;primary_key"` //
	ThirdUserId int    `json:"thirdUserId" gorm:"type:int(11);"`            //
	MemberId    int    `json:"memberId" gorm:"type:int(11);"`               //
	Status      int    `json:"status" gorm:"type:tinyint(1);"`              // 状态 1 有效， 0 无效
	DataScope   string `json:"dataScope" gorm:"-"`
	Params      string `json:"params"  gorm:"-"`
	BaseModel
}

func (MemberThirdInfo) TableName() string {
	return "member_third_info"
}

// 创建MemberThirdInfo
func (e *MemberThirdInfo) Create() (MemberThirdInfo, error) {
	var doc MemberThirdInfo
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取MemberThirdInfo
func (e *MemberThirdInfo) Get() (MemberThirdInfo, error) {
	var doc MemberThirdInfo
	table := orm.Eloquent.Table(e.TableName())
	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}
	if e.ThirdUserId != 0 {
		table = table.Where("third_user_id = ?", e.ThirdUserId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.Status != 0 {
		table = table.Where("status = ?", e.Status)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取MemberThirdInfo带分页
func (e *MemberThirdInfo) GetPage(pageSize int, pageIndex int) ([]MemberThirdInfo, int, error) {
	var doc []MemberThirdInfo

	table := orm.Eloquent.Select("*").Table(e.TableName())
	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}
	if e.ThirdUserId != 0 {
		table = table.Where("third_user_id = ?", e.ThirdUserId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.Status != 0 {
		table = table.Where("status = ?", e.Status)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

func (e *MemberThirdInfo) InsertUpdate(tuid, mid int) (update MemberThirdInfo, err error) {
	var memberThird MemberThirdInfo
	err = orm.Eloquent.Table("member_third_info").Where("third_user_id = ? AND status = 1", tuid).Find(&memberThird).Error
	if err != nil {
		//新增记录
		memberThird.ThirdUserId = tuid
		memberThird.MemberId = mid
		memberThird.Status = 1
		update, err = memberThird.Create()
		return update, err
	}

	//更新记录
	memberThird.MemberId = mid
	memberThird.Status = 1
	update, err = memberThird.Update(memberThird.Id)
	return update, err
}

// 更新MemberThirdInfo
func (e *MemberThirdInfo) Update(id int) (update MemberThirdInfo, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除MemberThirdInfo
func (e *MemberThirdInfo) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&MemberThirdInfo{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *MemberThirdInfo) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&MemberThirdInfo{}).Error; err != nil {
		return
	}
	Result = true
	return
}
